package com.jinwei.qunguanjia.web.system.controller;

import com.jinwei.qunguanjia.common.annotation.Log;
import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.enums.BusinessTypeEnum;
import com.jinwei.qunguanjia.common.utils.PageDataUtil;
import com.jinwei.qunguanjia.common.utils.PageUtils;
import com.jinwei.qunguanjia.system.dto.SysNoticeDTO;
import com.jinwei.qunguanjia.system.service.SysNoticeService;
import com.jinwei.qunguanjia.web.system.vo.SysNoticeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 公告 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController {
    @Resource
    private SysNoticeService sysNoticeService;

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermission('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/save")
    public Response add(@Validated @RequestBody SysNoticeVO sysNoticeVO) {
        SysNoticeDTO sysNoticeDTO = new SysNoticeDTO();
        BeanUtils.copyProperties(sysNoticeVO, sysNoticeDTO);
        sysNoticeService.save(sysNoticeDTO);
        return Response.success();
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermission('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/update")
    public Response edit(@Validated @RequestBody SysNoticeVO sysNoticeVO) {
        SysNoticeDTO sysNoticeDTO = new SysNoticeDTO();
        BeanUtils.copyProperties(sysNoticeVO, sysNoticeDTO);
        sysNoticeService.update(sysNoticeDTO);
        return Response.success();
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermission('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/remove/{noticeIds}")
    public Response remove(@PathVariable String[] noticeIds) {
        sysNoticeService.remove(Arrays.asList(noticeIds));
        return Response.success();
    }

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermission('system:notice:list')")
    @GetMapping("/list")
    public PageDataInfo<SysNoticeVO> list(SysNoticeVO sysNoticeVO) throws IllegalAccessException {
        SysNoticeDTO sysNoticeDTO = new SysNoticeDTO();
        BeanUtils.copyProperties(sysNoticeVO, sysNoticeDTO);
        PageUtils.startPage();
        PageDataInfo<SysNoticeDTO> pageDataInfo = sysNoticeService.listPage(sysNoticeDTO);
        PageDataUtil<SysNoticeDTO, SysNoticeVO> pageDataUtil = new PageDataUtil<>();
        return pageDataUtil.transform(pageDataInfo, SysNoticeVO.class);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public Response<SysNoticeVO> getInfo(@PathVariable String noticeId) {
        SysNoticeDTO sysNoticeDTO = sysNoticeService.get(noticeId);
        SysNoticeVO sysNoticeVO = new SysNoticeVO();
        BeanUtils.copyProperties(sysNoticeDTO, sysNoticeVO);
        return Response.success(sysNoticeVO);
    }
}
